package com.github.thinwonton.activemq.showcase.cluster.mq;

import com.alibaba.fastjson.JSONObject;
import com.github.thinwonton.activemq.showcase.cluster.entity.Email;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

public class Producer {

    private JmsTemplate jmsTemplate;

    /**
     * 发送消息.
     *
     * @param mail
     */
    public void sendMessage(final Email mail) {
        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(JSONObject.toJSONString(mail));
            }
        });
    }

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
}
